package system;
import java.io.IOException;
import java.util.Scanner;

import state.Globals;
import state.group.ElevatorStruct;

import managers.ElevatorManager;
import managers.HallCallManager;
import managers.strategy.DistanceStrategy;
import network.managers.ObserverManager;
import network.server.ConnectionListenerThread;
import network.server.GroupDispatcherServerThread;

public class GroupDispatcherMain {
    
    public static void printIntro()
    {
        System.out.println("Usage: ");
        System.out.println("Java -jar GroupDispatcher.jar <max floors> <port>");
        System.out.println("or");
        System.out.println("Java -jar GroupDispatcher.jar <port>");
        System.out.println("Both arguments are integers");
        
        System.exit(0);
    }
    
    private static ConnectionListenerThread t = null;
    
    public static void initSystem(int floors, int port)
    {
    	try
        {
            Globals.initLogger("Group Dispatcher");
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
        
        ElevatorManager.init((byte) floors);
        HallCallManager.init(new DistanceStrategy());
        ObserverManager.init();
        
        try
        {
            t = new GroupDispatcherServerThread(port);
        }
        catch (IOException e)
        {
            Globals.getLogger().error("Could not start up Group DispatcherServerThread", e);
        }
        
        if (t != null)
        {
        	t.start();
        }
    }
    
    public static void shutdownSystem()
    {
    	
    	if (t != null)
    	{
    		t.shutdownThread();
    	}
    	
    	ElevatorManager.shutdown();
        HallCallManager.shutdown();
        ObserverManager.shutdown();
        
        try
        {
            Globals.getLogger().closeLogger();
        }
        catch (IOException e)
        {
            Globals.getLogger().error("Could not close the logger", e);
        }
        
        System.out.println("Shutting down");
        
        System.exit(0);
    	
    }
    
    public static void main(String[] args)
    {
        int floors = 0;
        int port = 0;
        
        System.out.println("Elevator System Group Dispatcher");
        
        if (args.length == 0)
        {
            printIntro();
        }
        else if (args.length == 1)
        {
            floors = ElevatorStruct.MAX_FLOORS;
            try
            {
                port = Integer.parseInt(args[0]);
            }
            catch (IllegalArgumentException e)
            {
                System.out.println("Could not parse an Integer out of the argument, check usage");
                System.out.println();
                printIntro();
            }
        }
        else if (args.length == 2)
        {
            
            try
            {
                floors = Integer.parseInt(args[0]);
                port = Integer.parseInt(args[1]);
            }
            catch (IllegalArgumentException e)
            {
                System.out.println("Could not parse an Integer out of the arguments, check usage");
                System.out.println();
                printIntro();
            }
        }
        else
        {
            System.out.println("Received too many arguments, check usage!");
            
            printIntro();
        }
        
        initSystem(floors, port);
        
        if (t != null)
        {
            System.out.println("Initialization complete, press enter to shut down");
            
            Scanner input = new Scanner(System.in);
            input.nextLine();
        }
        else
        {
            System.out.println("Error occurred during initialization, shutting down");
        }
        
        shutdownSystem();
        
    }
    
}
